﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.Text;
using System.Web.UI;
using ReportTracking.ReportTrackingDTT;
using System.Data;

namespace ReportTracking.Services
{
    /// <summary>
    /// Summary description for CbDVKDVT
    /// </summary>
    public class CbDVKDVT : IHttpHandler, IReadOnlySessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                context.Response.ContentType = "text/plain";
                DateTime fd = Convert.ToDateTime(context.Request.QueryString["fd"]);
                DateTime td = Convert.ToDateTime(context.Request.QueryString["td"]);
                StringBuilder sData = new StringBuilder();
                TrackingReportClient rptrk = new TrackingReportClient();
                DataSet ds = rptrk.GetBusinessUnit(fd, td);
                if (ds != null)
                {
                    sData.Append("[{");
                    sData.Append("'id': 0,");
                    sData.Append("'text': 'All',");
                    sData.Append("'iconCls':'icon-ok',");
                    sData.Append("'checked':false,");
                    sData.Append("'children':[");
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        sData.Append("{");
                        sData.Append("'id':");
                        sData.Append("'");
                        sData.Append((i + 1));
                        sData.Append("',");
                        sData.Append("'text':");
                        sData.Append("'");
                        sData.Append(ds.Tables[0].Rows[i][0]);
                        sData.Append("',");
                        sData.Append("'checked':false,");
                        sData.Append("}");
                        sData.Append(",");
                    }
                    sData.Replace(",", "", sData.Length - 2, 1);
                    sData.Append("]");
                }
                else
                {
                    sData.Append("[{");
                    sData.Append("'id': 0,");
                    sData.Append("'text': 'All',");
                    sData.Append("'iconCls':'icon-ok',");
                }

                sData.Append("}]");
                context.Response.Write(Newtonsoft.Json.JsonConvert.DeserializeObject(sData.ToString()));
            }
            catch (Exception)
            {
                    
                throw;
            }
            

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}